//
//  WorkerCompleteTableViewCell.m
//  work
//
//  Created by 徐宏飞 on 16/6/3.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "WorkerCompleteTableViewCell.h"
#import "WorkerCompleteCollectionViewCell.h"


static NSString *collectionViewCell_ID = @"WorkerCompleteCollectionViewCell";


@interface WorkerCompleteTableViewCell ()<
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property (nonatomic, strong) UICollectionView *collectionViewImages;  //上传图片的View
@property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout;  //格式化
@property (nonatomic, copy) NSString *imageURL;
@property (nonatomic, assign) BOOL isShow;



@end


@implementation WorkerCompleteTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self addSubview:self.collectionViewImages];
        self.selectedPhotos = [NSMutableArray array];
        
    }
    return self;
}





- (void)deleteBtnClik:(UIButton *)sender {
  
    _imageURL  = _selectedPhotos[sender.tag-2];
    [_selectedPhotos removeObjectAtIndex:sender.tag-2];
    
    //    _layout.itemCount = _selectedPhotos.count;
    
    [self.collectionViewImages performBatchUpdates:^{
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
        [self.collectionViewImages deleteItemsAtIndexPaths:@[indexPath]];
    } completion:^(BOOL finished) {
        [self.collectionViewImages reloadData];
        
    }];
    
    //吧修改的数组传回去
    [self.delegate returnChangededPhotos:_selectedPhotos withRemoveImage:_imageURL];
    
}



#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectedPhotos.count + 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkerCompleteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell_ID forIndexPath:indexPath];
    
    
    if (indexPath.row == 0) {
        
        cell.imgComplete.image = FW_IMAGE(@"take-photo");
        cell.deleteBtn.hidden = YES;
        
        
    }else if(indexPath.row == 1){
        
        cell.imgComplete.image = FW_IMAGE(@"delete");
        cell.deleteBtn.hidden = YES;

        
    } else {
        NSString *str = self.selectedPhotos[indexPath.row - 2];
        NSURL *url = [NSURL URLWithString:[str stringByAppendingString:avatarcase]];
        [cell.imgComplete sd_setImageWithURL:url placeholderImage:AVATARPLACEHOLDER];
        if (_isShow) {
            cell.deleteBtn.hidden = NO;

        }else{
        cell.deleteBtn.hidden = YES;
        }

    }
    
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        
        [self.delegate WorkerCompleteCellClick:indexPath];
        
        
    }else if(indexPath.row == 1){
    
        _isShow = !_isShow;
        [self.delegate reloadUI];

        
    }
    else{
        if(!_isShow){
            [self.delegate WorkerCompleteReturnArray:self.selectedPhotos andIndex:indexPath.row - 2];
        }
        
        return;
    }
}




- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath didMoveToIndexPath:(NSIndexPath *)destinationIndexPath {
//    if (sourceIndexPath.item == 0 || destinationIndexPath.item == 0) return;
//    UIImage *image = _selectedPhotos[sourceIndexPath.item];
//    if (image) {
//        [_selectedPhotos exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
//        [_selectedAssets exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
//        [self.collectionViewImages reloadData];
//    }
}







#pragma mark - gettsers and setters
- (UICollectionView *)collectionViewImages{
    if (!_collectionViewImages) {
        
        //item 比例4:5
        CGSize size = CGSizeMake(118, 118);
        //4  4s  5  5s
        if (SCREEN_WIDTH == 320) {
            size = CGSizeMake(86, 86);
        }
        
        //6 6s
        if (SCREEN_WIDTH == 375) {
            size = CGSizeMake(105, 105);
        }
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.itemSize = size;
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
        self.flowLayout.minimumLineSpacing = 12;
        self.flowLayout.minimumInteritemSpacing = 8;

        _collectionViewImages = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250*WID) collectionViewLayout:self.flowLayout];
        
        _collectionViewImages.backgroundColor = UIColorFromRGB(0xEBEBEB);
        _collectionViewImages.delegate = self;
        _collectionViewImages.dataSource = self;
        [_collectionViewImages registerNib:[UINib nibWithNibName:@"WorkerCompleteCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionViewCell_ID];
        
    }
    return _collectionViewImages;
}


#pragma mark - setter and getter
- (void)setSelectedPhotos:(NSMutableArray *)selectedPhotos{
    _selectedPhotos = selectedPhotos;
    [self.collectionViewImages reloadData];
}
//
//- (void)setSelectedAssets:(NSMutableArray *)selectedAssets{
//    _selectedPhotos = selectedAssets;
//    [self.collectionViewImages reloadData];
//}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
